package service

import (
    "context"
    "gitee.com/WisdomClassroom/CollegeStruct/glb"
    "gitee.com/WisdomClassroom/core"
    "gitee.com/WisdomClassroom/core/models"
    "gitee.com/WisdomClassroom/core/protobufs/v1/pb"
    "github.com/jinzhu/gorm"
    "time"
)

func (s *Service) UnSetCollegeManager(_ context.Context, in *pb.UnSetCollegeManagerRequest) (*pb.UnSetCollegeManagerResponse, error) {
    token := core.NewToken()
    err := token.UnpackToken(in.AuthToken, &core.FlagTokenCert)
    if err != nil {
        return &pb.UnSetCollegeManagerResponse{Status: &pb.ResponseStatus{
            Code: pb.ResponseStatusCode_NotAuth, Error: err.Error(),
        },}, nil
    }

    if token.Type != core.UserRoleTypeCodeRoot {
        return &pb.UnSetCollegeManagerResponse{Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_OtherError,
            Message: "用户没有权限",
        },}, nil
    }
    userOfReq := models.UserModel{
        Model: models.Model{
            UUID: token.UUID,
        },
        Type: core.UserRoleTypeCodeRoot,
    }
    err = glb.DB.Scopes(notDeleted).Where(&userOfReq).First(&userOfReq).Error
    if err == gorm.ErrRecordNotFound {
        return &pb.UnSetCollegeManagerResponse{Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_OtherError,
            Message: "用户没有权限或编号不存在",
        },}, nil
    } else if err != nil {
        glb.Logger.Error(err.Error())
        return unsetCollegeManagerRespSvrErr, nil
    }

    if len(in.WillUnSetCollegeUUID) != 36 {
        return &pb.UnSetCollegeManagerResponse{Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_OtherError,
            Message: "学院编号不正确",
        },}, nil
    }
    if len(in.WillUnSetCollegeUUID) != 36 {
        return &pb.UnSetCollegeManagerResponse{Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_OtherError,
            Message: "管理员编号不正确",
        },}, nil
    }

    collegeMngrBindWithUUID := models.CollegeManagerBindModel{
        CollegeUUID: in.WillUnSetCollegeUUID,
        ManagerUUID: in.WillUnSetManagerUUID,
    }
    txCollegeManagerBindUUID := glb.DB.Where(collegeMngrBindWithUUID)
    err = txCollegeManagerBindUUID.Scopes(notDeleted).First(&collegeMngrBindWithUUID).Error
    if err == gorm.ErrRecordNotFound {
        return &pb.UnSetCollegeManagerResponse{Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_OtherError,
            Message: "此管理员绑定组合不存在",
        },}, nil
    } else if err != nil {
        glb.Logger.Error(err.Error())
        return unsetCollegeManagerRespSvrErr, nil
    }

    collegeManagerBind := models.CollegeManagerBindModel{
        Model: models.Model{
            Deleted:    true,
            DeleteTime: time.Now().Unix(),
        },
    }
    err = txCollegeManagerBindUUID.Model(&collegeManagerBind).Updates(collegeManagerBind).Error
    if err != nil {
        glb.Logger.Error(err.Error())
        return unsetCollegeManagerRespSvrErr, nil
    }
    return &pb.UnSetCollegeManagerResponse{
        Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_Suc},
    }, nil
}
